#ifndef __SPLIT_TOOL_HPP__
#define __SPLIT_TOOL_HPP__

#include "../cppjieba/include/cppjieba/Jieba.hpp" //引入第三方库

#include <vector>
#include <string>
#include <set>

using std::string;
using std::vector;
using std::set;

namespace gcj
{

//设计成单例类，避免每次创建WebPage对象的时候都生成一个jieba，影响速度
class SplitTool
{
public:
    vector<string> cut(const string str);
    set<string> & getStopWords();
    static SplitTool* getInstance() {
        //返回指向本类型的指针
        if(_pInstance == nullptr) {
            _pInstance = new SplitTool();
        }
        return _pInstance;
    }
private:
    SplitTool();
    ~SplitTool() {  }

private:
    cppjieba::Jieba _jieba;         //jieba对象，用来切词
    set<string> _stopWords;         //停止词
    static SplitTool * _pInstance;  //指向本类型的指针

private:
    //利用友元类实现单例对象的自动释放
    class AutoRelease {
    public:
        AutoRelease() {  }
        ~AutoRelease() {
            //实现单例对象的自动回收
            std::cout << "~AutoRelease()" << std::endl;
            if(SplitTool::_pInstance == nullptr) {
                return;
            }
            delete SplitTool::_pInstance;
            SplitTool::_pInstance = nullptr;
        }
    };
private:
    static AutoRelease _at; //用来联动回收资源的内部类对象应该保存在栈上
};

/* //饿汉模式，直接生成 */
/* SplitTool *SplitTool::_pInstance = SplitTool::getInstance(); */
/* SplitTool::AutoRelease SplitTool::_at; */

}//end of namespace gjc

#endif
